This tutorial illustrates the core visualization utilities available in Ax.
import numpy as np
from ax.service.ax_client import AxClient
from ax.modelbridge.cross_validation import cross_validate
from ax.plot.contour import interact_contour
from ax.plot.diagnostic import interact_cross_validation
from ax.plot.scatter import(
interact_fitted,
plot_objective_vs_constraints,
tile_fitted,
)
from ax.plot.slice import plot_slice
from ax.utils.measurement.synthetic_functions import hartmann6
from ax.utils.notebook.plotting import render, init_notebook_plotting
init_notebook_plotting()
[INFO 06-08 05:19:41] ax.utils.notebook.plotting: Injecting Plotly library into cell. Do not overwrite or delete cell.
The vizualizations require an experiment object and a model fit on the evaluated data. The routine below is a copy of the Service API tutorial, so the explanation here is omitted. Retrieving the experiment and model objects for each API paradigm is shown in the respective tutorials
noise_sd = 0.1
param_names = [f"x{i+1}" for i in range(6)] # x1, x2, ..., x6
def noisy_hartmann_evaluation_function(parameterization):
x = np.array([parameterization.get(p_name) for p_name in param_names])
noise1, noise2 = np.random.normal(0, noise_sd, 2)
return {
"hartmann6": (hartmann6(x) + noise1, noise_sd),
"l2norm": (np.sqrt((x ** 2).sum()) + noise2, noise_sd)
}
ax_client = AxClient()
ax_client.create_experiment(
name="test_visualizations",
parameters=[
{
"name": p_name,
"type": "range",
"bounds": [0.0, 1.0],
}
for p_name in param_names
],
objective_name="hartmann6",
minimize=True,
outcome_constraints=["l2norm <= 1.25"]
)
[INFO 06-08 05:19:41] ax.service.ax_client: Starting optimization with verbose logging. To disable logging, set the `verbose_logging` argument to `False`. Note that float values in the logs are rounded to 6 decimal points.
[INFO 06-08 05:19:41] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x1. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 06-08 05:19:41] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x2. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 06-08 05:19:41] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x3. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 06-08 05:19:41] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x4. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 06-08 05:19:41] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x5. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 06-08 05:19:41] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x6. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 06-08 05:19:41] ax.service.utils.instantiation: Created search space: SearchSpace(parameters=[RangeParameter(name='x1', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x2', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x3', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x4', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x5', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x6', parameter_type=FLOAT, range=[0.0, 1.0])], parameter_constraints=[]).
[INFO 06-08 05:19:41] ax.modelbridge.dispatch_utils: Using Bayesian optimization since there are more ordered parameters than there are categories for the unordered categorical parameters.
[INFO 06-08 05:19:41] ax.modelbridge.dispatch_utils: Using Bayesian Optimization generation strategy: GenerationStrategy(name='Sobol+GPEI', steps=[Sobol for 12 trials, GPEI for subsequent trials]). Iterations after 12 will take longer to generate due to model-fitting.
for i in range(20):
parameters, trial_index = ax_client.get_next_trial()
# Local evaluation here can be replaced with deployment to external system.
ax_client.complete_trial(trial_index=trial_index, raw_data=noisy_hartmann_evaluation_function(parameters))
[INFO 06-08 05:19:41] ax.service.ax_client: Generated new trial 0 with parameters {'x1': 0.669252, 'x2': 0.226343, 'x3': 0.100532, 'x4': 0.905644, 'x5': 0.968817, 'x6': 0.350246}.
[INFO 06-08 05:19:41] ax.service.ax_client: Completed trial 0 with data: {'hartmann6': (-0.078058, 0.1), 'l2norm': (1.377501, 0.1)}.
[INFO 06-08 05:19:41] ax.service.ax_client: Generated new trial 1 with parameters {'x1': 0.338428, 'x2': 0.287862, 'x3': 0.950846, 'x4': 0.901498, 'x5': 0.066311, 'x6': 0.848025}.
[INFO 06-08 05:19:41] ax.service.ax_client: Completed trial 1 with data: {'hartmann6': (-0.513942, 0.1), 'l2norm': (1.591014, 0.1)}.
[INFO 06-08 05:19:42] ax.service.ax_client: Generated new trial 2 with parameters {'x1': 0.933226, 'x2': 0.530677, 'x3': 0.920665, 'x4': 0.185797, 'x5': 0.900378, 'x6': 0.058709}.
[INFO 06-08 05:19:42] ax.service.ax_client: Completed trial 2 with data: {'hartmann6': (0.087262, 0.1), 'l2norm': (1.826409, 0.1)}.
[INFO 06-08 05:19:42] ax.service.ax_client: Generated new trial 3 with parameters {'x1': 0.425659, 'x2': 0.765519, 'x3': 0.025783, 'x4': 0.48311, 'x5': 0.179263, 'x6': 0.560975}.
[INFO 06-08 05:19:42] ax.service.ax_client: Completed trial 3 with data: {'hartmann6': (-0.325184, 0.1), 'l2norm': (1.149336, 0.1)}.
[INFO 06-08 05:19:42] ax.service.ax_client: Generated new trial 4 with parameters {'x1': 0.699956, 'x2': 0.125349, 'x3': 0.409858, 'x4': 0.739037, 'x5': 0.562755, 'x6': 0.374642}.
[INFO 06-08 05:19:42] ax.service.ax_client: Completed trial 4 with data: {'hartmann6': (0.134829, 0.1), 'l2norm': (1.225775, 0.1)}.
[INFO 06-08 05:19:42] ax.service.ax_client: Generated new trial 5 with parameters {'x1': 0.384704, 'x2': 0.065547, 'x3': 0.781597, 'x4': 0.509583, 'x5': 0.145384, 'x6': 0.017604}.
[INFO 06-08 05:19:42] ax.service.ax_client: Completed trial 5 with data: {'hartmann6': (-0.135038, 0.1), 'l2norm': (0.982512, 0.1)}.
[INFO 06-08 05:19:42] ax.service.ax_client: Generated new trial 6 with parameters {'x1': 0.378323, 'x2': 0.767966, 'x3': 0.305105, 'x4': 0.935053, 'x5': 0.316432, 'x6': 0.595768}.
[INFO 06-08 05:19:42] ax.service.ax_client: Completed trial 6 with data: {'hartmann6': (-0.139572, 0.1), 'l2norm': (1.362844, 0.1)}.
[INFO 06-08 05:19:42] ax.service.ax_client: Generated new trial 7 with parameters {'x1': 0.704705, 'x2': 0.370122, 'x3': 0.15055, 'x4': 0.977938, 'x5': 0.578094, 'x6': 0.072481}.
[INFO 06-08 05:19:42] ax.service.ax_client: Completed trial 7 with data: {'hartmann6': (-0.031993, 0.1), 'l2norm': (1.335024, 0.1)}.
[INFO 06-08 05:19:42] ax.service.ax_client: Generated new trial 8 with parameters {'x1': 0.437854, 'x2': 0.80328, 'x3': 0.816136, 'x4': 0.826897, 'x5': 0.350524, 'x6': 0.977305}.
[INFO 06-08 05:19:42] ax.service.ax_client: Completed trial 8 with data: {'hartmann6': (-0.23477, 0.1), 'l2norm': (1.858673, 0.1)}.
[INFO 06-08 05:19:42] ax.service.ax_client: Generated new trial 9 with parameters {'x1': 0.27378, 'x2': 0.628382, 'x3': 0.568427, 'x4': 0.154488, 'x5': 0.00633, 'x6': 0.648839}.
[INFO 06-08 05:19:42] ax.service.ax_client: Completed trial 9 with data: {'hartmann6': (-0.431958, 0.1), 'l2norm': (0.94791, 0.1)}.
[INFO 06-08 05:19:42] ax.service.ax_client: Generated new trial 10 with parameters {'x1': 0.960271, 'x2': 0.761758, 'x3': 0.284376, 'x4': 0.695549, 'x5': 0.983866, 'x6': 0.136461}.
[INFO 06-08 05:19:42] ax.service.ax_client: Completed trial 10 with data: {'hartmann6': (0.069841, 0.1), 'l2norm': (1.753371, 0.1)}.
[INFO 06-08 05:19:42] ax.service.ax_client: Generated new trial 11 with parameters {'x1': 0.211401, 'x2': 0.789832, 'x3': 0.151595, 'x4': 0.498257, 'x5': 0.918658, 'x6': 0.236677}.
[INFO 06-08 05:19:42] ax.service.ax_client: Completed trial 11 with data: {'hartmann6': (-0.961591, 0.1), 'l2norm': (1.379903, 0.1)}.
[INFO 06-08 05:19:58] ax.service.ax_client: Generated new trial 12 with parameters {'x1': 0.187054, 'x2': 0.789967, 'x3': 0.14434, 'x4': 0.431007, 'x5': 0.73182, 'x6': 0.276001}.
[INFO 06-08 05:19:58] ax.service.ax_client: Completed trial 12 with data: {'hartmann6': (-0.438356, 0.1), 'l2norm': (1.333733, 0.1)}.
[INFO 06-08 05:20:19] ax.service.ax_client: Generated new trial 13 with parameters {'x1': 0.189034, 'x2': 0.757946, 'x3': 0.105484, 'x4': 0.416062, 'x5': 0.952551, 'x6': 0.402898}.
[INFO 06-08 05:20:19] ax.service.ax_client: Completed trial 13 with data: {'hartmann6': (0.019768, 0.1), 'l2norm': (1.299269, 0.1)}.
[INFO 06-08 05:20:44] ax.service.ax_client: Generated new trial 14 with parameters {'x1': 0.143372, 'x2': 0.722792, 'x3': 0.069367, 'x4': 0.542421, 'x5': 0.958465, 'x6': 0.221532}.
[INFO 06-08 05:20:44] ax.service.ax_client: Completed trial 14 with data: {'hartmann6': (-0.656203, 0.1), 'l2norm': (1.369971, 0.1)}.
[INFO 06-08 05:21:16] ax.service.ax_client: Generated new trial 15 with parameters {'x1': 0.186816, 'x2': 0.769012, 'x3': 0.208259, 'x4': 0.349844, 'x5': 0.95003, 'x6': 0.223071}.
[INFO 06-08 05:21:16] ax.service.ax_client: Completed trial 15 with data: {'hartmann6': (-0.420389, 0.1), 'l2norm': (1.249151, 0.1)}.
[INFO 06-08 05:21:51] ax.service.ax_client: Generated new trial 16 with parameters {'x1': 0.255865, 'x2': 0.672325, 'x3': 0.261856, 'x4': 0.508379, 'x5': 0.878642, 'x6': 0.227617}.
[INFO 06-08 05:21:51] ax.service.ax_client: Completed trial 16 with data: {'hartmann6': (-0.95462, 0.1), 'l2norm': (1.457213, 0.1)}.
[INFO 06-08 05:22:41] ax.service.ax_client: Generated new trial 17 with parameters {'x1': 0.22102, 'x2': 0.96722, 'x3': 0.203437, 'x4': 0.51189, 'x5': 0.874275, 'x6': 0.220987}.
[INFO 06-08 05:22:41] ax.service.ax_client: Completed trial 17 with data: {'hartmann6': (-0.873725, 0.1), 'l2norm': (1.484153, 0.1)}.
[INFO 06-08 05:23:19] ax.service.ax_client: Generated new trial 18 with parameters {'x1': 0.198691, 'x2': 0.441027, 'x3': 0.215975, 'x4': 0.496107, 'x5': 0.779584, 'x6': 0.207389}.
[INFO 06-08 05:23:19] ax.service.ax_client: Completed trial 18 with data: {'hartmann6': (-0.112084, 0.1), 'l2norm': (1.224118, 0.1)}.
[INFO 06-08 05:23:44] ax.service.ax_client: Generated new trial 19 with parameters {'x1': 0.316629, 'x2': 0.648796, 'x3': 0.433555, 'x4': 0.296946, 'x5': 0.031187, 'x6': 0.597115}.
[INFO 06-08 05:23:44] ax.service.ax_client: Completed trial 19 with data: {'hartmann6': (-0.443196, 0.1), 'l2norm': (0.848788, 0.1)}.
The plot below shows the response surface for hartmann6 metric as a function of the x1, x2 parameters.
The other parameters are fixed in the middle of their respective ranges, which in this example is 0.5 for all of them.
# this could alternately be done with `ax.plot.contour.plot_contour`
render(ax_client.get_contour_plot(param_x="x1", param_y="x2", metric_name='hartmann6'))
[INFO 06-08 05:23:44] ax.service.ax_client: Retrieving contour plot with parameter 'x1' on X-axis and 'x2' on Y-axis, for metric 'hartmann6'. Remaining parameters are affixed to the middle of their range.
The plot below allows toggling between different pairs of parameters to view the contours.
model = ax_client.generation_strategy.model
render(interact_contour(model=model, metric_name='hartmann6'))
This plot illustrates the tradeoffs achievable for 2 different metrics. The plot takes the x-axis metric as input (usually the objective) and allows toggling among all other metrics for the y-axis.
This is useful to get a sense of the pareto frontier (i.e. what is the best objective value achievable for different bounds on the constraint)
render(plot_objective_vs_constraints(model, 'hartmann6', rel=False))
CV plots are useful to check how well the model predictions calibrate against the actual measurements. If all points are close to the dashed line, then the model is a good predictor of the real data.
cv_results = cross_validate(model)
render(interact_cross_validation(cv_results))
Slice plots show the metric outcome as a function of one parameter while fixing the others. They serve a similar function as contour plots.
render(plot_slice(model, "x2", "hartmann6"))
Tile plots are useful for viewing the effect of each arm.
render(interact_fitted(model, rel=False))
Total runtime of script: 4 minutes, 26.11 seconds.